﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using LinQ_DTO;
using System.Xml;

namespace EFashion
{
    public partial class SanPham : System.Web.UI.Page
    {
        public int LoaiSanPham;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            String thm;
            thm = (string)Session["themeSelected"];
            if (thm != null)
            {
                Page.Theme = thm;
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/config.xml"));
                XmlNode root = doc.DocumentElement;
                String theme = root.SelectSingleNode("theme").ChildNodes[0].Value.Trim();
                Session.Add("themeSelected", theme);
                Page.Theme = theme;
            }

        }



        protected void Page_Load(object sender, EventArgs e)
        {
            LoaiSanPhamBUS LoaiSpBus = new LoaiSanPhamBUS();
            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_loaisanpham> ListLoai = LoaiSpBus.LayLoaiSanPham();
            

            string LoaiSp = Request.QueryString["LoaiSP"];
            if (LoaiSp == "")
                LoaiSanPham = 0;
            else
                LoaiSanPham = Convert.ToInt32(Request.QueryString["LoaiSP"]);

            BindData_grvSPHotNam();
            BindData_grvSPHotNu();
            BindData_grvSPKMNam();
            BindData_grvSPKMNu();
            BindData_grvSPHienCoNam();
            BindData_grvSPHienCoNu();
            

           
        }

        protected void btnLoaiSP_Click(object sender, EventArgs e)
        {
            LinkButton sdr = (LinkButton)sender;
            SanPhamBUS SpBus = new SanPhamBUS();
            
            int MaLoaiSP = Convert.ToInt32(sdr.CommandArgument);
            List<shop_sanpham> DsSPTheoLoai = SpBus.LaySPTheoLoai(MaLoaiSP);

        }

        private void BindData_grvSPHienCo()
        {
            //Lay danh sach cac san pham host, bind vao dgrSpHienCo
            
            //SanPhamBUS SpBus = new SanPhamBUS();
            //List<shop_sanpham> ListSP = SpBus.GetAllProduct();
            //grvSPHienCo.DataSource = ListSP;
            //grvSPHienCo.DataKeyNames = new string[] { "MaSanPham" };
            //grvSPHienCo.DataBind();

        }

        protected void grvSPHienCo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SanPhamBUS SpBus = new SanPhamBUS();
                List<shop_sanpham> ListSP = SpBus.GetAllProduct();
                Image ImgControl = (Image)e.Row.FindControl("imgSPHC");
                shop_sanpham Sp = (shop_sanpham)e.Row.DataItem;//(shop_sanpham)ListSP[e.Row.];
                ImgControl.AlternateText = Sp.HinhSanPham.Element("HinhTruoc").Value;
                ImgControl.ImageUrl = Sp.HinhSanPham.Element("HinhTruoc").Value;
                
                
            }
            
        }

        //Dung de bound du lieu vao cac field phuc tap: Image, Khuyen Mai
        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                shop_sanpham Sp = (shop_sanpham)e.Row.DataItem;//(shop_sanpham)ListSP[e.Row.];
                
                //bind image
                Image ImgControl = (Image)e.Row.FindControl("imgSP");
                ImgControl.AlternateText = Sp.HinhSanPham.Element("HinhTruoc").Value;
                ImgControl.ImageUrl = Sp.HinhSanPham.Element("HinhTruoc").Value;

                //bind Khuyen Mai
                Label KM = (Label)e.Row.FindControl("lbKM");
                string NoiDungKM = string.Empty;
                foreach (shop_sanpham_khuyenmai Km in Sp.shop_sanpham_khuyenmais)
                {
                    NoiDungKM += Km.shop_khuyenmai.NoiDung;
                }
                if (NoiDungKM == "")
                    KM.Text = "Không";
                else
                    KM.Text = NoiDungKM ;

            }
        }

        //protected void grvSPHienCo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        //{
        //         grvSPHienCo.PageIndex = e.NewSelectedIndex;
        //         grvSPHienCo.EditIndex = -1;
        //         grvSPHienCo.SelectedIndex = -1;
        //}

        //protected void grvSPHienCo_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    BindData_grvSPHienCo();
        //}

        protected void grvSPHienCo_PageIndexChanged(object sender, EventArgs e)
        {
            BindData_grvSPHienCo();
        }

        protected void grvSPHienCo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
            
        }

        protected void grvSPHotNu_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void BindData_grvSPHotNam()
        {
            //Lay danh sach cac san pham host cua nam, bind vao dgrSpHotNam
            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> ListSP;
            if (LoaiSanPham == 0)
                ListSP = SpBus.LaySanPhamHotTheoPhai(true);
            else
                ListSP = SpBus.LaySanPhamHotTheoLoaiPhai(true, LoaiSanPham);

            this.grvSPHotNam.DataSource = ListSP;
            grvSPHotNam.DataKeyNames = new string[] { "MaSanPham" };
            grvSPHotNam.DataBind();

        }

        private void BindData_grvSPHotNu()
        {
            //Lay danh sach cac san pham host Nu, bind vao dgrSpHotNu

            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> ListSP;
            if (LoaiSanPham == 0)
                ListSP = SpBus.LaySanPhamHotTheoPhai(false);
            else
                ListSP = SpBus.LaySanPhamHotTheoLoaiPhai(false, LoaiSanPham);

            this.grvSPHotNu.DataSource = ListSP;
            grvSPHotNu.DataKeyNames = new string[] { "MaSanPham" };
            grvSPHotNu.DataBind();

        }

        private void BindData_grvSPKMNam()
        {
            //Lay danh sach cac san pham KM cua nam, bind vao dtvSPKMNam
            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> ListSP;
            if (LoaiSanPham == 0)
                ListSP = SpBus.LaySanPhamKhuyenMaiTheoPhai(true);
            else
                ListSP = SpBus.LaySanPhamKhuyenMaiTheoLoaiPhai(true, LoaiSanPham);

            this.grvSPKMNam.DataSource = ListSP;
            grvSPKMNam.DataKeyNames = new string[] { "MaSanPham" };
            grvSPKMNam.DataBind();
        }

        private void BindData_grvSPKMNu()
        {
            //Lay danh sach cac san pham KM cua nam, bind vao dtvSPKMNam
            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> ListSP;
            if (LoaiSanPham == 0)
                ListSP = SpBus.LaySanPhamKhuyenMaiTheoPhai(false);
            else
                ListSP = SpBus.LaySanPhamKhuyenMaiTheoLoaiPhai(false, LoaiSanPham);

            this.grvSPKMNu.DataSource = ListSP;
            grvSPKMNu.DataKeyNames = new string[] { "MaSanPham" };
            grvSPKMNu.DataBind();
        }

        private void BindData_grvSPHienCoNam()
        {
            //Lay danh sach cac san pham KM cua nam, bind vao dtvSPKMNam
            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> ListSP;
            if (LoaiSanPham == 0)
                ListSP = SpBus.LaySanPhamTheoPhai(true);
            else
                ListSP = SpBus.LaySanPhamTheoLoaiPhai(true, LoaiSanPham);

            this.grvSPNam.DataSource = ListSP;
            grvSPNam.DataKeyNames = new string[] { "MaSanPham" };
            grvSPNam.DataBind();
        }

        private void BindData_grvSPHienCoNu()
        {
            //Lay danh sach cac san pham KM cua nam, bind vao dtvSPKMNam
            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> ListSP;
            if (LoaiSanPham == 0)
                ListSP = SpBus.LaySanPhamTheoPhai(false);
            else
                ListSP = SpBus.LaySanPhamTheoLoaiPhai(false, LoaiSanPham);

            this.grvSPNu.DataSource = ListSP;
            grvSPNu.DataKeyNames = new string[] { "MaSanPham" };
            grvSPNu.DataBind();
        }

        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ListSP.aspx?TenDS=HotNam&LoaiSP=" + LoaiSanPham.ToString());
        }

        protected void grvSPHotNam_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

    }
}
